var api = require('../../config/api.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    customerCode: '',
    workOrderList: [],
    workOrderState: 0,
    mPageNum: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.workOrderState = options.workOrderState

    this.doHttpGetWorkOrderFindAllByCondition()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    var that = this
    that.setData({
      mPageNum: 1
    })
    that.doHttpGetWorkOrderFindAllByCondition()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    var that = this
    let num = that.data.mPageNum
    that.setData({
      mPageNum: num += 1
    });
    that.doHttpGetWorkOrderFindAllByCondition()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 获取我的工单列表
   */
  doHttpGetWorkOrderFindAllByCondition: function () {
    wx.getStorage({
        key: 'psCustomerCode',
        success: (res) => {
          this.setData({
            customerCode: res.data
          });
            var that = this
            wx.request({
            url: api.workOrderFindAllByCondition,
            method: 'POST',
            data: {
                customerCode: that.data.customerCode,
                feedbackType: 3,
                workOrderState: parseInt(that.data.workOrderState),
                pageNum: that.data.mPageNum,
                pageSize: 5
            },
            success: function (res) {
                if (that.data.mPageNum != 1) {
                that.setData({
                    workOrderList: that.data.workOrderList.concat(res.data.data.rows)
                });
                } else {
                wx.stopPullDownRefresh()
                that.setData({
                    workOrderList: res.data.data.rows
                });
                }
            }
            })
        }
    })
  },

  toWorkOrderDetails: function (e) {
    wx.navigateTo({
        url: "/pages/MyWorkOrder/orderDetail/orderDetail?workOrderCode="+ e.currentTarget.dataset.idtype.workOrderCode,
      });
  },
})